package cn.yy.cloudpay.util;

import java.io.BufferedInputStream;
import java.io.BufferedReader;
import java.io.ByteArrayInputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.FileWriter;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.io.UnsupportedEncodingException;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Properties;
import java.util.Random;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import org.xml.sax.SAXException;

import android.content.Context;
import android.content.pm.PackageInfo;
import android.content.pm.PackageManager;

public class StringUtil {


	/**
	 * 字符串是否为空（不排除字符串左右空格）
	 * 
	 * @param source
	 * @return
	 */
	public static boolean isEmpty(String source) {
		if (source == null || source.length() == 0)
			return true;
		return false;
	}

	/**
	 * 字符串是否为空（会排除字符串左右空格）
	 * 
	 * @param source
	 * @return
	 */
	public static boolean isEmptyStr(String source) {
		if (source == null || source.trim().length() == 0)
			return true;
		return false;
	}

	/**
	 * 字符串数据是否为空（会排除掉字符串左右的空格） add by tangj 20130121
	 * 
	 * @param source
	 * @param isTrim
	 *            是否忽略空格
	 * @return
	 */
	public static boolean isStringArrayEmpty(String[] source, boolean isTrim) {
		if (isTrim) {
			boolean isEmpty = true;
			if (source != null) {
				int length = source.length;
				for (int i = 0; i < length; i++) {
					if (!isEmptyStr(source[i])) {
						isEmpty = false;
						break;
					}
				}
			}
			return isEmpty;
		} else {
			return isStringArrayEmpty(source);
		}
	}

	/**
	 * 字符串数组是否存在空字符串
	 * 
	 * @param source
	 * @return
	 */
	public static boolean isStringArrayEmpty(String[] source) {
		if (source == null || source.length == 0)
			return true;
		return false;
	}

	/**
	 * 是否以逗号为分割的字符串
	 * 
	 * @param source
	 * @return
	 */
	public static boolean isSplitType(String source) {
		if (source != null && source.lastIndexOf(",") != -1
				&& source.lastIndexOf(",") > 0)
			return true;
		return false;
	}

	/**
	 * 判断是否以指定字符来分割的字符串
	 * 
	 * @param source
	 * @param sym
	 * @return
	 */
	public static boolean isSplitType(String source, String sym) {
		if (source != null && source.lastIndexOf(sym) != -1
				&& source.lastIndexOf(sym) > 0)
			return true;
		return false;
	}

	/**
	 * 是否是数值，不带小数和负数
	 * 
	 * @param str
	 * @return
	 */
	public final static boolean isNumeric(String str) {
		Pattern pattern = Pattern.compile("[0-9]*");
		Matcher isNum = pattern.matcher(str);
		if (!isNum.matches()) {
			return false;
		}
		return true;
	}

	/**
	 * 是否是数值，带小数，带负数
	 * 
	 * @param str
	 * @return
	 */
	public final static boolean isNumber(String str) {
		Pattern pattern = Pattern.compile("^-{0,1}\\d+(\\.\\d+)?$");
		Matcher isNum = pattern.matcher(str);
		if (!isNum.matches()) {
			return false;
		}
		return true;
	}

	/**
	 * 自定义正则判断字符串
	 * 
	 * @param str
	 * @param regx
	 * @return
	 */
	public final static boolean isValidateByRegx(String str, String regx) {
		Pattern pattern = Pattern.compile(regx);
		Matcher isNum = pattern.matcher(str);
		if (!isNum.matches()) {
			return false;
		}
		return true;
	}

	/**
	 * 是否为字母组成的字符串
	 * 
	 * @param str
	 * @return
	 */
	public final static boolean isString(String str) {
		Pattern pattern = Pattern.compile("[a-z|A-Z]*");
		Matcher isNum = pattern.matcher(str);
		if (!isNum.matches()) {
			return false;
		}
		return true;
	}

	/**
	 * 是否为字母和数字组成的字符串
	 * 
	 * @param source
	 * @return
	 */
	public final static boolean isLetterChar(String source) {
		if (isEmptyStr(source))
			return false;
		Pattern pattern = Pattern.compile("[a-z|A-Z|0-9]*");
		Matcher matcher = pattern.matcher(source);
		return matcher.matches();
	}

	/**
	 * 转换空字符串为空值
	 * 
	 * @param source
	 * @return
	 */
	public static String transEmptyStr(String source) {
		if (source == null || source.trim().length() == 0)
			return "";
		return source;
	}

	/**
	 * 字符串数组是否存在空字符串
	 * 
	 * @param source
	 * @return
	 */
	public static boolean isEmptyStrs(String[] source) {
		if (source == null)
			return true;
		int len = source.length;
		int i = 0;
		boolean isEmptyStr = true;
		if (!isStringArrayEmpty(source)) {
			for (String str : source) {
				if (isEmptyStr(str)) {
					i++;
				}
			}
			if (i != len) {
				isEmptyStr = false;
			}
		}
		return isEmptyStr;
	}

	/**
	 * 是否为空并为数字
	 * 
	 * @param para
	 * @return
	 */
	public static boolean isEmptyAndNumber(String para) {
		return !isEmptyStr(para) && isNumeric(para);
	}

	/**
	 * 获取指定长度的一组随机数
	 * 
	 * @param length
	 * @return
	 */
	public static String genRandomString(int length) {
		final int maxNum = 36;
		int i;
		int count = 0;
		char[] str = { 'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k',
				'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w',
				'x', 'y', 'z', '0', '1', '2', '3', '4', '5', '6', '7', '8', '9' };
		StringBuffer buffer = new StringBuffer("");
		Random r = new Random();
		while (count < length) {
			i = Math.abs(r.nextInt(maxNum));
			if (i >= 0 && i < str.length) {
				buffer.append(str[i]);
				count++;
			}
		}
		return buffer.toString();
	}

	/**
	 * 字符串转Long数组
	 * 
	 * @param str
	 * @param format
	 * @return
	 */
	public static long[] stringToLongArray(String str, String format) {
		if (StringUtil.isEmpty(str))
			return null;
		if (StringUtil.isEmpty(format))
			return null;
		String temp[] = str.split(format);
		if (temp != null && temp.length != 0 && temp[0].length() != 0) {
			long longArray[] = new long[temp.length];
			for (int i = 0; i < temp.length; i++)
				longArray[i] = Long.parseLong(temp[i].trim());
			return longArray;
		} else
			return null;
	}

	/**
	 * 数字字符串转list集合
	 * 
	 * @param str
	 * @return
	 */
	public static List<Long> splitToLongList(String str) {
		if (StringUtil.isEmptyStr(str) || !isSplitType(str))
			return null;
		List<Long> targetList = new ArrayList<Long>();
		String temp[] = str.split(",");
		for (String t : temp) {
			try {
				targetList.add(Long.parseLong(t));
			} catch (NumberFormatException e) {
				e.printStackTrace();
			}
		}
		return targetList;
	}

	/**
	 * 是否有效日期
	 * 
	 * @param str
	 * @param format
	 * @return
	 */
	public static boolean isValidDate(String str, String format) {
		SimpleDateFormat df = new SimpleDateFormat(format);
		df.setLenient(false);
		try {
			df.parse(str);
			return true;
		} catch (ParseException e) {
			return false;
		}
	}

	/**
	 * 是否有效日期
	 * 
	 * @param regex
	 * @return
	 */
	public static boolean isValidDateRegex(String regex) {
		Matcher yearMatcher = Pattern.compile("yyyy").matcher(regex);
		Matcher monthMatcher = Pattern.compile("MM").matcher(regex);
		Matcher dateMatcher = Pattern.compile("dd").matcher(regex);
		if (yearMatcher.find() && !yearMatcher.find() && monthMatcher.find()
				&& !monthMatcher.find() && dateMatcher.find()
				&& !dateMatcher.find()) {
			return true;
		}
		return false;
	}

	/**
	 * 是否有效时间
	 * 
	 * @param regex
	 * @return
	 */
	public static boolean isValidTimeRegex(String regex) {
		Matcher hourMatcher = Pattern.compile("HH").matcher(regex);
		Matcher minuteMatcher = Pattern.compile("mm").matcher(regex);
		Matcher secondMatcher = Pattern.compile("ss").matcher(regex);
		if (hourMatcher.find() && !hourMatcher.find() && minuteMatcher.find()
				&& !minuteMatcher.find() && secondMatcher.find()
				&& !secondMatcher.find()) {
			return true;
		}
		return false;
	}

	/**
	 * 空格清除
	 * 
	 * @param source
	 * @return
	 */
	public static String replaceLineBreak(String source) {
		if (isEmptyStr(source))
			return source;
		String result = source.replace("\r\n", "<br/>");
		if (result.endsWith("<br/>")) {
			result = result.substring(0, result.lastIndexOf("<br/>"));
		}
		return result;
	}

	/**
	 * 空格清除
	 * 
	 * @param source
	 * @return
	 */
	public static String replaceLineBreakAndEmpty(String source) {
		if (isEmptyStr(source))
			return source;
		String result = source.replace("\r\n", "<br/>").replace(" ", "&nbsp;");
		if (result.endsWith("<br/>")) {
			result = result.substring(0, result.lastIndexOf("<br/>"));
		}
		return result;
	}

	/**
	 * 字符串验证是否是数字
	 * 
	 * @param source
	 * @return
	 */
	public static boolean isPositiveNumberStr(String source) {
		if (isNumber(source)) {
			double val = Double.parseDouble(source);
			if (val > 0)
				return true;
		}
		return false;
	}

	/**
	 * 字符串替换
	 * 
	 * @param str
	 * @param subStr
	 *            源字符串
	 * @param reStr
	 *            新字符串
	 * @return
	 */
	public static String replaceEx(String str, String subStr, String reStr) {
		if (str == null) {
			return null;
		}
		if ((subStr == null) || (subStr.equals(""))
				|| (subStr.length() > str.length()) || (reStr == null)) {
			return str;
		}
		StringBuffer sb = new StringBuffer();
		int lastIndex = 0;
		while (true) {
			int index = str.indexOf(subStr, lastIndex);
			if (index < 0) {
				break;
			}
			sb.append(str.substring(lastIndex, index));
			sb.append(reStr);

			lastIndex = index + subStr.length();
		}
		sb.append(str.substring(lastIndex));
		return sb.toString();
	}

	/**
	 * 字符串转MAP
	 * 
	 * @param str
	 *            “1，1|2,2”
	 * @param entrySpliter
	 * @param keySpliter
	 * @return
	 */
	public static Map splitToMapx(String str, String entrySpliter,
			String keySpliter) {
		Map map = new HashMap();
		String[] arr = splitEx(str, entrySpliter);
		for (int i = 0; i < arr.length; i++) {
			String[] arr2 = splitEx(arr[i], keySpliter);
			String key = arr2[0];
			if (isEmpty(key)) {
				continue;
			}
			key = key.trim();
			String value = null;
			if (arr2.length > 1) {
				value = arr2[1];
			}
			map.put(key, value);
		}
		return map;
	}

	/**
	 * 字符串转数组
	 * 
	 * @param str
	 * @param spilter
	 * @return
	 */
	public static String[] splitEx(String str, String spilter) {
		if (str == null) {
			return null;
		}
		if ((spilter == null) || (spilter.equals(""))
				|| (str.length() < spilter.length())) {
			String[] t = { str };
			return t;
		}
		ArrayList al = new ArrayList();
		char[] cs = str.toCharArray();
		char[] ss = spilter.toCharArray();
		int length = spilter.length();
		int lastIndex = 0;
		for (int i = 0; i <= str.length() - length;) {
			boolean notSuit = false;
			for (int j = 0; j < length; j++) {
				if (cs[(i + j)] != ss[j]) {
					notSuit = true;
					break;
				}
			}
			if (!notSuit) {
				al.add(str.substring(lastIndex, i));
				i += length;
				lastIndex = i;
			} else {
				i++;
			}
		}
		if (lastIndex <= str.length()) {
			al.add(str.substring(lastIndex, str.length()));
		}
		String[] t = new String[al.size()];
		for (int i = 0; i < al.size(); i++) {
			t[i] = ((String) al.get(i));
		}
		return t;
	}

	/**
	 * 首字母转小写
	 * 
	 * @param s
	 * @return
	 */
	public static String toLowerCaseFirstOne(String s) {
		if (Character.isLowerCase(s.charAt(0)))
			return s;
		else
			return (new StringBuilder())
					.append(Character.toLowerCase(s.charAt(0)))
					.append(s.substring(1)).toString();
	}

	/**
	 * 首字母转大写
	 * 
	 * @param s
	 * @return
	 */
	public static String toUpperCaseFirstOne(String s) {
		if (Character.isUpperCase(s.charAt(0)))
			return s;
		else
			return (new StringBuilder())
					.append(Character.toUpperCase(s.charAt(0)))
					.append(s.substring(1)).toString();
	}

	/**
	 * 产生序列号
	 * 
	 * @return
	 */
	public synchronized static Long getSeq() {
		Long seq = System.nanoTime();
		try {
			Thread.sleep(1);
		} catch (InterruptedException e) {
			e.printStackTrace();
		}
		String idStr = seq.toString();
		int length = idStr.length();
		if (idStr.length() > 15) {
			idStr = idStr.substring(length - 15, length);
		}
		// System.out.println("原：" + idStr);
		return Long.parseLong(idStr);
	}

	/**
	 * 获取当前用户系统目录
	 * 
	 * @return
	 */
	public static String projectPath() {
		return System.getProperty("user.dir");
	}

	/**
	 * 获取tomcat路径
	 * 
	 * @return
	 */
	public static String getTomcatPath() {
		String path = getPath();
		String tomcatPath = path.substring(0, path.indexOf(getProjectName()));
		return tomcatPath;
	}

	/**
	 * 获取工程路径
	 * 
	 * @return
	 */
	public static String getPath() {
		String projectname = StringUtil.class.getResource("/").getPath();
		String pn = projectname.substring(0, projectname.indexOf("WEB-INF"))
				.replaceAll("%20", " ");
		return pn;
	}

	/**
	 * 获取EXCEL映射文件
	 * 
	 * @return
	 */
	public static String getExcelPath() {
		String projectname = StringUtil.class.getResource("/").getPath();
		String pn = projectname + "com/yw/core/utils/xml/";
		return pn;
	}

	/**
	 * 获取当前工程名称
	 * 
	 * @return
	 */
	public static String getProjectName() {
		String projectname = StringUtil.class.getResource("/").getPath();
		String pn = projectname.substring(0, projectname.indexOf("WEB-INF"))
				.replaceAll("%20", " ");
		String path[] = pn.split("/");
		return path[path.length - 1];
	}


	/**
	 * 判断str长度是否超过max值
	 * 
	 * @param str
	 * @param max
	 * @return
	 */
	public static boolean stringIsMax(String str, int max) {
		int length = str.length();

		if (length > max) {
			return true;
		}

		return false;
	}

	/**
	 * 字符串转Long
	 * 
	 * @param str
	 * @return
	 */
	public static Long strToLong(String str) {
		if (str == null || str.length() == 0)
			return null;
		else {
			return Long.valueOf(str);
		}
	}

	/**
	 * 字符串转Double
	 * 
	 * @param str
	 * @return
	 */
	public static Double strToDouble(String str) {
		if (str == null || str.length() == 0)
			return null;
		else {
			return Double.valueOf(str);
		}
	}

	/**
	 * 获取当前操作系统类型
	 * 
	 * @return
	 */
	public static String getOsName() {
		String os = "";
		os = System.getProperty("os.name");
		return os;
	}

	/**
	 * 空字符串转空值
	 * 
	 * @param obj
	 * @return
	 */
	public static String toEmpty(String obj) {
		if (obj == null)
			return "";
		return obj;
	}

	/**
	 * 转绝对值
	 */
	public static String toABS(String str) {
		if (isEmpty(str))
			return "";
		String[] strs = str.split(",");
		StringBuffer newStr = new StringBuffer();
		for (String s : strs) {
			Long uid = -Long.valueOf(s);
			newStr.append(",").append(uid.toString());
		}
		if (newStr.length() > 0)
			str = newStr.substring(1);
		return str;
	}

	/**
	 * 数组转字符串
	 */
	public static String arrayToString(String[] str) {
		if (str == null)
			return "";
		StringBuffer rs = new StringBuffer();
		String rsStr = "";
		for (String s : str) {
			rs.append(",").append(s);
		}
		if (!isEmpty(rs.toString())) {
			rsStr = rs.substring(1);
		}
		return rsStr;
	}

	public static String generateCheckCode() {
		String chars = "0123456789";
		char[] rands = new char[6];
		for (int i = 0; i < 6; i++) {
			int rand = (int) (Math.random() * 10);
			rands[i] = chars.charAt(rand);
		}
		return new String(rands);
	}

	public static boolean isExist(String str, String[] cols) {
		if (isEmpty(str))
			return false;
		if (cols == null)
			return false;
		for (String s : cols) {
			if (str.equals(s)) {
				return true;
			}
		}
		return false;
	}

	/**
	 * 用= sql语句时候用这个替换
	 * 
	 * @param str
	 * @return
	 * @author wangzx
	 */
	public static String replaceSql4Equal(String str) {
		str = str.replaceAll("'", "''");
		return str;
	}

	/**
	 * 用like sql语句时候用这个替换
	 * 
	 * @param str
	 * @return
	 * @author wangzx
	 */
	public static String replaceSql4Like(String str) {
		// str = str.replaceAll("\", "\\"); //mysql 这个好像暂时用不上
		str = str.replaceAll("/", "//");
		str = str.replaceAll("\\[", "/[");
		str = str.replaceAll("%", "/%");
		str = str.replaceAll("_", "/_");
		str = str.replaceAll("\\^", "/^");
		return str;
	}

	public static String contactStr(String split, String... str) {
		String rs = "";
		for (String s : str) {
			if (!StringUtil.isEmpty(s)) {
				rs += "," + s;
			}
		}
		if (!StringUtil.isEmpty(rs)) {
			rs = rs.substring(1);
		}
		return rs;
	}

	public static String ajaxStr(String str) {
		String s = "";
		try {
			if (StringUtil.isEmpty(str)) {
				return str;
			}
			s = new String(str.getBytes("gbk"), "utf-8");
		} catch (UnsupportedEncodingException e) {
			e.printStackTrace();
		}
		return s;
	}

	
	public static String getExceptionDetail(Throwable e,List<String> exs) {		
		StringBuffer msg = new StringBuffer("null");
		if (e != null) {
			msg = new StringBuffer("");
			String message = e.toString();
			int length = e.getStackTrace().length;
			if (length > 0) {
				msg.append(message + "\n");
				for (int i = 0; i < length; i++) {
					String info = e.getStackTrace()[i].toString();
					if(info.indexOf("com.yw")!=-1){
						exs.add(info);
					}
				}				
			} else {
				msg.append(message);
			}
		}
		return msg.toString();
	}
	
	/**
	 * 读取系统配置文件
	 * @param attrName
	 * @return
	 */
		public static String readProperties(String attrName) {
			String attrValue = loadProperties(attrName);
			if(!StringUtil.isEmpty(attrValue))return attrValue;
			Properties p = new Properties();
			try {
				InputStream inputStream = StringUtil.class.getClassLoader()
						.getResourceAsStream("bocop.properties");
				p.load(inputStream);
				inputStream.close();
			} catch (Exception ex) {
				ex.printStackTrace();
			}
			return p.getProperty(attrName);
		}
	/**
	 * 加载插件项目配置文件	
	 * @param attrName
	 * @return
	 */
		public static String loadProperties(String attrName){
			Properties p = new Properties();
			try {
				String url = StringUtil.class.getClassLoader().getResource("/").getPath();
//				String path = url + "com/yw/";			
				//File f = new File(path+"/bocop.properties");
				InputStream fileInputSteam = new FileInputStream(url+"/bocop.properties");
				InputStream inputStream = new BufferedInputStream(fileInputSteam);
				p.load(inputStream);
				inputStream.close();
			} catch (Exception ex) {
				ex.printStackTrace();
			}
			return p.getProperty(attrName);
		}
	/**
	 * 读取指定配置文件名的属性值
	 * @param fileName
	 * @param attrName
	 * @return
	 */
		public static String readProperties(String fileName, String attrName) {
			Properties p = new Properties();
			try {
				InputStream inputStream = StringUtil.class.getClassLoader()
						.getResourceAsStream(fileName);
				p.load(inputStream);
				inputStream.close();
			} catch (Exception ex) {
				ex.printStackTrace();
			}
			return p.getProperty(attrName);
		}
	/**
	 * 写配置文件
	 * @param fileName
	 * @param attrName
	 * @param attrValue
	 */
		public static void writeProperties(String fileName, String[] attrName,
				String[] attrValue) {
			Properties p = new Properties();
			try {
				OutputStream outputStream = new FileOutputStream(fileName);
				p.load(new FileInputStream(fileName));
				int i = 0;
				for (String key : attrName) {
					p.setProperty(key, attrValue[i]);
					i++;
				}
				p.store(outputStream, "");
				outputStream.close();
			} catch (Exception ex) {
				ex.printStackTrace();
			}

		}
		
		 /**
	     * 将元为单位的转换为分 替换小数点，支持以逗号区分的金额
	     *
	     * @param amount
	     * @return
	     */
	    public static String changeY2F(String amount) {
	        String currency = amount.replaceAll("\\$|\\￥|\\,", "");  //处理包含, ￥ 或者$的金额
	        int index = currency.indexOf(".");
	        int length = currency.length();
	        Long amLong = 0l;
	        if (index == -1) {
	            amLong = Long.valueOf(currency + "00");
	        } else if (length - index >= 3) {
	            amLong = Long.valueOf((currency.substring(0, index + 3)).replace(".", ""));
	        } else if (length - index == 2) {
	            amLong = Long.valueOf((currency.substring(0, index + 2)).replace(".", "") + 0);
	        } else {
	            amLong = Long.valueOf((currency.substring(0, index + 1)).replace(".", "") + "00");
	        }
	        return amLong.toString();
	    }
	    
	    /**
		 * 获取版本号
		 * @return 当前应用的版本号
		 */
		public static String getVersion(Context context) {
		    try {
		        PackageManager manager = context.getPackageManager();
		        PackageInfo info = manager.getPackageInfo(context.getPackageName(), 0);
		        String version = "V "+info.versionName;
		        return version;
		    } catch (Exception e) {
		        e.printStackTrace();
		        return "无法获取版本号";
		    }
		}

	public static String getCurrentDate(){
		SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		java.util.Date date=new java.util.Date();
		String str=sdf.format(date);
		return str;
	}

	public static String genseq() {
		String date = new SimpleDateFormat("yyMMdd").format(new Date());
		String seconds = new SimpleDateFormat("HHmmss").format(new Date());
		Long seq = System.nanoTime();
		String s = seq.toString().substring(6);
		String rs = date + seconds + s;
		return rs;
	}

	/**
	 * 描述：将null转化为“”.
	 *
	 * @param str
	 *            指定的字符串
	 * @return 字符串的String类型
	 */
	public static String parseEmpty(String str) {
		if (str == null || "null".equals(str.trim())) {
			str = "";
		}
		return str.trim();
	}

	public static String byte2HexStr(byte[] src){
		StringBuilder stringBuilder = new StringBuilder("");
		if (src == null || src.length <= 0) {
			return null;
		}
		for (int i = 0; i < src.length; i++) {
			int v = src[i] & 0xFF;
			String hv = Integer.toHexString(v);
			if (hv.length() < 2) {
				stringBuilder.append(0);
			}
			stringBuilder.append(hv);
		}
		return stringBuilder.toString();
	}
	/**
	 * Convert hex string to byte[]
	 * @param hexString the hex string
	 * @return byte[]
	 */
	public static byte[] hexStr2Bytes(String hexString) {
		if (hexString == null || hexString.equals("")) {
			return null;
		}
		hexString = hexString.toUpperCase();
		int length = hexString.length() / 2;
		char[] hexChars = hexString.toCharArray();
		byte[] d = new byte[length];
		for (int i = 0; i < length; i++) {
			int pos = i * 2;
			d[i] = (byte) (charToByte(hexChars[pos]) << 4 | charToByte(hexChars[pos + 1]));
		}
		return d;
	}
	/**
	 * Convert char to byte
	 * @param c char
	 * @return byte
	 */
	private static byte charToByte(char c) {
		return (byte) "0123456789ABCDEF".indexOf(c);
	}

	public static int byteArrayToInt(byte[] bytes) {
		int value=0;
		//由高位到低位
		for(int i = 0; i < 4; i++) {
			int shift= (4-1-i) * 8;
			value +=(bytes[i] & 0x000000FF) << shift;//往高位游
		}
		return value;
	}
}
